<?php
/**
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage http
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* http request data structure.
* @author J.Ducastel <nospam0@ducastel.name>
* @uses urlBean
*/
class HttpRequestBean extends DataBean {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
    /**
    * constructor alias
    * @access public
    * @param array $data associative array defining object properties
    */
    function HttpRequestBean($prop=array()) {
        $this->__construct($prop);
    }/**/

    /**
    * "real" constructor
    * @access public
    * @param array $prop associative array defining object properties
    */
    function __construct($current=true) {
        if ($current)
            $this->autoValues();
        $this->membersLock(true);
    }
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
    /**
    * builds and/or returns current request instance
    * @static
    * @access public
    * @return wakHttpRequest by reference
    */
    function & getCurrent() {
        static $current;
        if (!$current) {
            $current=&new HttpRequestBean(true);
        }
        return $current;
    }
/*------------------------------------------------------------------------------
                                                             getters and setters
------------------------------------------------------------------------------*/
    /**
    * @internal
    */
    function makeID() {
        return $this->method
        .'.'.str_replace('/','-',$this->url->getrelative());
    }

    /**
    * getter for $method
    * @return string
    */
    function getmethod() {
        return $this->get('method');
    }/**/
    /**
    * setter for $method
    * @param string $value
    * @return bool
    */
    function setmethod($value) {
        return $this->set('method',$value);
    }/**/

    /**
    * getter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function getprotocol() {
        return $this->get('protocol');
    }/**/
    /**
    * setter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function setprotocol($value) {
        return $this->set('protocol',$value);
    }/**/

    /**
    * getter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function getServerPort() {
        return $this->get('server_port');
    }/**/
    /**
    * setter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function setServerPort($value) {
        return $this->set('server_port',$value);
    }/**/

    /**
    * getter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function getReferer() {
        return $this->get('referer');
    }/**/
    /**
    * setter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function setReferer($value) {
        return $this->set('referer',$value);
    }/**/

    /**
    * getter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function getClientIP() {
        return $this->get('client_ip');
    }/**/
    /**
    * setter for $protocol
    * @param string $value optional. if null, return current value
    * @return bool|string
    */
    function setClientIp($value) {
        return $this->set('client_ip',$value);
    }/**/


    /**
    * return request relative uri
    * @return string
    */
    function getUri() {
        return $this->url->get('path');
    }/**/

    /**
    * return segment N from uri, Nth part between slashes
    * @access public
    * @param int $index
    * @return string
    */
    function getUriSegment($index) {
        return $this->url->getUriSegment($index);
    }/**/

    /**
    * return base href = scheme+host+(basepath)
    * @return string
    */
    function getBaseHref() {
        return $this->url->getscheme().'://'
        .$this->url->gethost()
        .($this->url->getbasepath()?('/'.$this->url->getbasepath()):'')
        .'/';
    }/**/

    /**
    * return base path
    * @return string
    */
    function getBasePath() {
        return $this->url->getbasepath();
    }

    /**
     * tells if method is POST
     * @return bool
     */
    function isPost() {
        return $this->method=='POST';
    }

/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/

    /**
    * @var string GET|POST
    */
    var $method;
    function _method_inputfilter($x) {
        return strtoupper($x);
    }
    function _method_check($x) {
        return in_array($x,array('GET','POST','PUT','DELETE'));
    }
    function _method_autoValue() {
        return $_SERVER['REQUEST_METHOD'];
    }

    /**
    * @var string SERVER_PROTOCOL
    */
    var $protocol;
    function _protocol_check($x) {
        return in_array($x,array('HTTP/1.0','HTTP/1.1','HTTPS/1.0'));
    }
    function _protocol_autoValue() {
        return $_SERVER['SERVER_PROTOCOL'];
    }

    /**
    * @var int server port
    */
    var $server_port;
    function _server_port_inputfilter($x) {
        return intval($x);
    }
    function _server_port_autoValue() {
        return $_SERVER['SERVER_PORT'];
    }
    //var $_server_port_check_type='integer';
    /**
    * @var string client/browser IP address
    */
    var $client_ip;
    function _client_ip_autoValue() {
        return $_SERVER['REMOTE_ADDR'];
    }
    /**
    * checker for client_ip
    * @return bool
    */
    function _client_ip_check($value) {
        $ereg_0_255='(1?([0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))';
        $ereg_ip=$ereg_0_255.'\.'.$ereg_0_255.'\.'.$ereg_0_255.'\.'.$ereg_0_255;
        return ereg($ereg_ip,$value);
    }/**/
    /**
    * @var string HTTP_REFERER
    */
    var $referer;
    function _referer_autoValue() {
        return isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:NULL;
    }
    /**
    * @var array HTTP_ACCEPT parsed values
    */
    var $accept; //
    function _accept_inputfilter($value) {
        if (is_string($value)) $value=$this->_parseHttpAccept($value);
        return $value;
    }
    function _accept_check($x) {
        return is_array($x);
    }
    function _accept_autoValue() {
        return $this->_parseHttpAccept($_SERVER['HTTP_ACCEPT']);
    }

    /**
    * @var array HTTP_ACCEPT_LANGUAGE parsed values
    */
    var $accept_language; // HTTP_ACCEPT_LANGUAGE
    function _accept_language_inputfilter($value) {
        if (is_string($value)) $value=$this->_parseHttpAccept($value);
        return $value;
    }
    function _accept_language_check($x) {
        return is_array($x);
    }
    function _accept_language_autoValue() {
        return isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])?$this->_parseHttpAccept($_SERVER['HTTP_ACCEPT_LANGUAGE']):NULL;
    }
    /**
    * @var HTTP_CACHE_CONTROL
    */
    var $cache_control;
    function _cache_control_autoValue() {
        isset($_SERVER['HTTP_CACHE_CONTROL'])?$_SERVER['HTTP_CACHE_CONTROL']:NULL;
    }
    /**
    * @var HTTP_USER_AGENT;
    */
    var $user_agent;
    function _user_agent_autoValue() {
        return isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:NULL;
    }
    /**
     * @var array
     */
    var $get;
    function _get_get() {
        return $_GET;
    }

    /**
    * @var array _POST
    */
    var $post;
    function _post_get() {
        return $_POST;
    }
    /**
    * @var array _COOKIE
    */
    var $cookie;
    function _cookie_get() {
        return $_COOKIE;
    }

    /**
     * @var array
     */
    var $files;
    function _files_get() {
        return $_FILES;
    }
    /**
    * @var string
    */
    // var $
    /**
    * @var urlbean instance of urlbean to store url-related data
    */
    var $url;
    function _url_inputfilter($x) {
        if (!is_a($x,'urlbean'))
            $x=& new urlbean($x);
        return $x;
    }
    function _url_autoValue() {
        $protocol=$_SERVER['SERVER_PROTOCOL'];
        ereg('^([a-zA-Z]+)',$protocol,$found);
        $scheme=strtolower($found[1]);
        $url=$scheme.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        // defining base path to current application
        $base=dirname($_SERVER['PHP_SELF']);
        // dirty hack for some web hosts (namely, ovh)
        if (substr($base,0,strlen($_SERVER['DOCUMENT_ROOT']))==$_SERVER['DOCUMENT_ROOT'])
            $base=substr($base,strlen($_SERVER['DOCUMENT_ROOT']));
        // echo "<p>$url, $base</p>";
        return new urlBean($url,$base);
    }
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
    /**
    * parses http accept strings
    * @access private
    * @param string $str
    * @return array sorted values, higher coefficient to lower
    */
    function _parseHttpAccept($str) {
        // getting http instruction
        // $str=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
        // exploding accepted languages
        $chunks=explode(',',$str);
        // creating output list
        $accepted=array();
        foreach ($chunks as $order => $chunk) {
            // parsing chunks
            // 2_digit_code[-longer_code][;q=coefficient]
            ereg('([^;]+)(;q=([0-9\.]+))?',$chunk,$found);
            $value=$found[1];
            // coefficient
            $coef=sprintf('%3.1f',$found[3]?$found[3]:'1');
            // for sorting by coefficient
            $key=$coef.'.'.sprintf('%2d',99-$order);
            // adding
            $accepted[$key]=$value;
        }
        // sorting the list by coefficient desc
        krsort($accepted);
        return $accepted;
    }
/*------------------------------------------------------------------------------

------------------------------------------------------------------------------*/
}
?>
